import { handlerGobackClick } from "../../../utils/util";
import { Message } from "../../../models/message";

const message = new Message();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    result: {},
    username: "",
    options: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options,
    });

    if (options.id) {
      this.setData({
        id: options.id,
        options,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initPageData();
    let username = wx.getStorageSync("user").username
      ? wx.getStorageSync("user").username
      : "用户";
    this.setData({
      username,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  handlerGobackClick,
  initPageData() {
    message
      .getById(this.data.id)
      .then((res) => {
        this.setData({
          result: res.result,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    message
      .changeMsgStatus(this.data.id)
      .then((res) => {})
      .catch((err) => {
        console.log(err);
      });
  },
  goTarget(e) {
    let { url, id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `${url}?id=${id}`,
    });
  },
});
